using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// Summary description for LogWriter
/// </summary>
public static class LogWriter
{
    static string strLogFile = "";
    public static void WriteLog(string strLog)
    {
        try
        {
            strLogFile = GetLogFileName();

            if (HttpContext.Current.Session["user_name"] != null)
            {
                if (HttpContext.Current.Session["user_name"].ToString() != "")
                {
                    string sUserName = HttpContext.Current.Session["user_name"].ToString();
                    removeInvalidChars(ref sUserName);
                    if (strLogFile.IndexOf(sUserName) == -1)
                        strLogFile = GetLogFileName();
                }
            }
            StreamWriter SW = new StreamWriter(strLogFile, true);
            SW.WriteLine(DateTime.Now.ToString() + " : " + strLog);
            SW.Close();
        }
        catch (Exception e)
        {

        }
    }
    private static void removeInvalidChars(ref string inp)
    {
        try
        {
            if (inp.Length < 1) return;
            char[] removeChars = new char[] { '\\', '/', ':', '?', '"', '<', '>', '|', ' ' };
            string ret = inp;
            for (int i = 0; i < removeChars.Length; i++)
                inp = inp.Replace(removeChars[i].ToString(), "");
        }
        catch (Exception e)
        { }
    }
    /// <summary>
    /// Generate the file name to store the log.
    /// Name is hardcoded for time being.... Logfile code will be enhanced to store based on the date
    /// </summary>
    /// <returns></returns>
    private static string GetLogFileName()
    {
        string strRet = "";
        string strFileName = "";
        string strAppPath = "";
        string appendString = "";
        strAppPath = System.Web.HttpContext.Current.Request.ApplicationPath;
        strAppPath = System.Web.HttpContext.Current.Server.MapPath(strAppPath);
        if (strAppPath.LastIndexOf(@"\") != -1)
        {
            strAppPath += @"\";
        }
        strAppPath += "Log";
        DirectoryInfo di = new DirectoryInfo(strAppPath);
        if (di.Exists == false)
        {
            di.Create();
        }
        if (Convert.ToInt32(DateTime.Now.Month) > 9)
        {
            strFileName = DateTime.Now.Month.ToString();
        }
        else
        {
            strFileName = "0" + DateTime.Now.Month.ToString();
        }

        if (Convert.ToInt32(DateTime.Now.Day) > 9)
        {
            strFileName += DateTime.Now.Day.ToString();
        }
        else
        {
            strFileName += "0" + DateTime.Now.Day.ToString();
        }

        strFileName += DateTime.Now.Year.ToString();

        try
        {
            appendString = HttpContext.Current.Session["user_name"] == null ? "" : HttpContext.Current.Session["user_name"].ToString();
        }
        catch (Exception e)
        {        }

        removeInvalidChars(ref appendString);

        strRet = strAppPath + @"\" + appendString + strFileName + ".log";
        return strRet;
    }
}
